package gat.common.util;


import cn.hutool.core.util.StrUtil;
import gat.common.exception.ExceptionEnum;

public class PasswordCheckUtil {

    //定义特殊字符
    public static String SPECIAL_CHAR = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";


    public static void checkPassword(String password){
        Integer count = 0;

        boolean b1 = checkContainDigit(password);
        if (b1){
            count++;
        }
        boolean b2 = checkContainsLetters(password);
        if (b2){
            count++;
        }
        boolean b3 = checkContainSpecialChar(password);
        if (b3){
            count++;
        }
        if(checkPasswordLength(password, "6", "20")){
            count++;
        }
        if (count < 3){
            ExceptionUtil.throwNew(ExceptionEnum.EXCEPTION_ENUM_10027);
        }

    }

    /**
     * @brief   检测密码中字符长度
     * @param[in] password            密码字符串
     * @return  符合长度要求 返回true
     */
    public static boolean checkPasswordLength(String password, String minNum, String maxNum) {
        boolean flag =false;
        if (StrUtil.isBlank(maxNum))  {
            minNum = StrUtil.isBlank(minNum) ? "0":minNum;
            if (password.length() >= Integer.parseInt(minNum)) {
                flag = true;
            }
        } else {
            minNum = StrUtil.isBlank(minNum) ? "0":minNum;
            if (password.length() >= Integer.parseInt(minNum) &&
                    password.length() <= Integer.parseInt(maxNum)) {
                flag = true;
            }
        }
        return flag;
    }

 /**
     * @brief   检测密码中是否包含数字
     * @param[in] password            密码字符串
     * @return  包含数字 返回true
     */
    public static boolean checkContainDigit(String password) {
        char[] chPass = password.toCharArray();
        for (int i = 0; i < chPass.length; i++) {
            if (Character.isDigit(chPass[i])) {
                return true;
            }
        }
        return false;
    }


    /**
     * @brief   检测密码中是否包含字母
     * @param[in] password            密码字符串
     * @return  包含字母 返回true
     */
    public static boolean checkContainsLetters(String password) {
        char[] chPass = password.toCharArray();
        for (int i = 0; i < chPass.length; i++) {
            if (Character.isLetter(chPass[i])) {
                return true;
            }
        }
        return false;
    }




    /**
     * @brief   检测密码中是否包含特殊符号
     * @param[in] password            密码字符串
     * @return  包含特殊符号 返回true
     */
    public static boolean checkContainSpecialChar(String password) {
        char[] chPass = password.toCharArray();
        for (int i = 0; i < chPass.length; i++) {
            if (SPECIAL_CHAR.indexOf(chPass[i]) != -1) {
                return true;
                
            }
        }
        return false;
    }


}

